package com.home766.controller.pad;

import com.home766.util.DateTime;
import net.biz.Page;
import net.biz.Pageable;
import net.biz.controller.shop.BaseController;
import net.biz.entity.Order;
import net.biz.service.OrderService;
import net.biz.service.SearchService;
import net.biz.util.JsonUtils;
import net.biz.util.ParamsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

/**
 * pad端-首页controller
 */
@Controller
@RequestMapping("/pad")
public class PadIndexController extends BaseController {
	
	@Autowired
	private OrderService orderService;
	@Resource(name="searchServiceImpl")
	private SearchService searchService;
	
	/**
	 * pad首页
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/index",method = RequestMethod.GET)
	public String index(ModelMap model){
		return "/pad/index";
	}
	
	/**
	 * pad最新预约
	 * @param pageable
	 * @return
	 */
	@RequestMapping(value="/newAppoint",method = RequestMethod.GET,produces=("text/plain; charset=utf-8"))
	@ResponseBody
	public String newAppoint(Pageable pageable){
		String hours=" 00:00:00 ";
		Date d=new Date();
//		String 
		String startstr=DateTime.dateTimeToString(d);
		String endstr=DateTime.dateTimeToString(DateTime.getNextDay(new Date()));
		startstr=startstr.split(" ")[0]+hours;
		endstr=endstr.split(" ")[0]+hours;
		Date start=DateTime.getDate(startstr, DateTime.FULL_DATE_PATTERN);
		Date end=DateTime.getDate(endstr, DateTime.FULL_DATE_PATTERN);
		Page<Order> orders = orderService.findPage(null,null,start,end,null,null, ParamsUtil.CART_TYPE_SCHEME,null,null,null,null,pageable,null,null,null,null,null,false);
		return JsonUtils.toJson(orders.getContent());
	}
	
	/**
	 * 搜索
	 */
	@RequestMapping(value = "/search", method = RequestMethod.GET)
	public String search(ModelMap model, String keyword, Pageable pageable) {
		model.addAttribute("page", searchService.search(keyword, null,null,null,pageable));
		model.addAttribute("productKeyword", keyword);
		return "/pad/product/list";
	}
}
